<?php

class CourseBatcheController extends GxController {

    public function actionSelectBatchDate() {
        $batchDates = '';
        $batchId = '';

        /* --------------------find all batch date by venueId---------------------- */
        $batchData = CourseBatche::model()->findAll('course_venue_id=:course_venue_id AND courses_id=:courses_id', array(':course_venue_id' => (int) $_POST['venueId'], 'courses_id' => (int) $_POST['courses_id']));
        foreach ($batchData as $value => $name) {
            $batchDates.= CHtml::tag('option', array('value' => $name->id), CHtml::encode($name->date_from . ' To ' . $name->date_to), true);
            $batchId.= $name->id . '#';
        }
        $batchId = rtrim($batchId, "#");
        echo $batchDates . "@@" . $batchId;
        /* --------------------find all batch date by venueId---------------------- */
    }

    public function actionSelectBatchTicket() {
        $batchTicket = '';
        /* --------------------find all batch time by Batch date---------------------- */
        $batchData = CourseBatche::model()->find('id=:id', array(':id' => (int) $_POST['id']));
        for ($i = 1; $i <= $batchData->seats; $i++) {
            $batchTicket.= CHtml::tag('option', array('value' => $i), CHtml::encode($i), true);
        }
        echo $batchTicket;
        /* --------------------find all batch time by Batch date---------------------- */
    }

    public function actionTotalprice() {
        $totalPrice = '';
        $seats = $_POST['seat'];
        $batchData = CourseBatche::model()->find('id=:id', array(':id' => (int) $_POST['batchId']));
        $price = $batchData->price * $seats;
        $price = number_format($price, 2);
        $start_date = $batchData->date_from;
        echo "Subtotal Rs. " . $price . '<br>';
        $earlyDiscountData = CourseEarlyDiscount::model()->find('course_batche_id=:course_batche_id', array(':course_batche_id' => (int) $_POST['batchId']));
        if (count($earlyDiscountData) > 0) {
            $valid_to = $earlyDiscountData->valid_to;
            $discount_price = $earlyDiscountData->discount_price;
            $in_percentage = $earlyDiscountData->in_percentage;
            /* Early Discout calculate */
            $currentDate = date('Y-m-d');
            if ($valid_to <> "" && $currentDate <= $valid_to) {
                if (empty($discount_price) <> true) {
                    echo "Early Discount Rs. " . number_format($discount_price, 2) . '<br>';
                    $totalPrice = $price - $discount_price;
                    $totalPrice = $totalPrice;
                } elseif (empty($in_percentage) <> true) {
                    $in_percentage = $price * ($in_percentage / 100);
                    echo "Early Discount Rs. " . number_format($in_percentage, 2) . '<br>';
                    $totalPrice = $price - $in_percentage;
                    $totalPrice = $totalPrice;
                }
            }
        } else {
            $totalPrice = $price;
            $totalPrice = $totalPrice;
        }

        /* Early Discout calculate */

        /* Group Discout calculate */
        $GroupDiscountData = CourseGroupDiscount::model()->find('course_batche_id=:course_batche_id', array(':course_batche_id' => (int) $_POST['batchId']));
        if (count($GroupDiscountData) > 0) {
            $group_size = $GroupDiscountData->group_size;
            $discount_amount = $GroupDiscountData->discount_amount;
            $discount_in_percentage = $GroupDiscountData->discount_in_percentage;

            if ($group_size <> "" && $group_size <= $seats) {
                if (empty($discount_amount) <> true) {
                    echo "Group Discount Rs. " . number_format($discount_amount, 2) . '<br>';
                    if ($totalPrice <> "") {
                        $totalPrice = $totalPrice - $discount_amount;
                    } else {
                        $totalPrice = $price - $discount_amount;
                    }
                    $totalPrice = $totalPrice;
                } elseif (empty($discount_in_percentage) <> true) {
                    if ($totalPrice <> "") {
                        $in_percentage = $price * ($discount_in_percentage / 100);
                        echo "Group Discount Rs. " . number_format($in_percentage, 2) . '<br>';
                        $totalPrice = $totalPrice - $in_percentage;
                        $totalPrice = $totalPrice;
                    } else {
                        $in_percentage = $price * ($discount_in_percentage / 100);
                        echo "Group Discount Rs. " . number_format($in_percentage, 2) . '<br>';
                        $totalPrice = $price - $in_percentage;
                        $totalPrice = $totalPrice;
                    }
                }
            }
        } else {
            $totalPrice = $price;
            $totalPrice = $totalPrice;
        }
        /* Group Discout calculate */
        echo '<div class="grand_total borB rempaddingB"></div>';
        echo "Grand Total Rs. " . number_format($totalPrice, 2);
    }

}